package com.eric.rabbit.entity.topic;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author:Eric
 * @date:2021/6/28
 */
@Slf4j
@Data
public class TopicSender {

    @Autowired
    RabbitTemplate rabbitTemplate;

    private static final String exchangeName = "topic.exchange";

    private final String[] keys = {"red", "yellow", "blue", "this.red", "red.to", "yellow.to.yes", "to.blue.to", "to.blue"};

    public void send(int i) {
        String key = keys[i % 8];
        String message = "topic{" + key + "}" + i;
        rabbitTemplate.convertAndSend(exchangeName, key, message);
        log.info("[x] sent key:{}  message:{} ", key, message);
    }


    public void sameKeySend(int i) {
        String key = keys[i % 8];
        String message = "topic{" + key + "}" + i;
        rabbitTemplate.convertAndSend("sameRouteKey.exchange", key, message);
        log.info("[x] sent key:{}  message:{} ", key, message);
    }
}
